package scheduler;

import javax.microedition.lcdui.*;

import commands.*;
import controllers.ScheduleController;
import core.*;

public class DayViewList extends List  implements CommandListener{

        private static final Command BackCmd = new Command(Commands.Exit, Command.BACK, 1);
        private static final Command AddCmd = new Command(Commands.Add, Command.ITEM, 1);
        private static final Command EditCmd = new Command(Commands.Edit, Command.ITEM, 1);
        private static final Command DeleteCmd = new Command(Commands.Remove, Command.ITEM, 1);
        private static final Command ReviewCmd = new Command(Commands.PairView, Command.ITEM, 1);
        private static final Command DayChoiceCmd = new Command(Commands.DayChoice, Command.ITEM, 1);
        private static final Command AboutCmd = new Command(Commands.About, Command.ITEM, 1);
              
        WeekDefinition m_week;
        DayOfWeek m_day;
        
        public DayViewList(){
                super("Day View", List.IMPLICIT);
                
                Dispatcher.getInstance().registerListener(this, Commands.DaySelected);
                Dispatcher.getInstance().registerListener(this, Commands.Updated);

                addCommand(BackCmd);
                if (super.size()!=0) 
                        addCommand(ReviewCmd);
                addCommand(AddCmd);
                addCommand(DayChoiceCmd);
                addCommand(AboutCmd);
                if (super.size()!=0)
                	addCommand(EditCmd);
                if (super.size()!=0)
                	addCommand(DeleteCmd);
                setCommandListener(this);
        }
        
        public void commandAction(Command cmd, Displayable display) {
                
                switch(cmd.getCommandType()){
                case Command.ITEM:
                        if (cmd.getLabel().equals(Commands.Add)){
                                Dispatcher.getInstance().handleCommand(cmd);
                        }
                        if (cmd.getLabel().equals(Commands.DaySelected)){
                                DaySelectedCommand daySelectedCmd = (DaySelectedCommand)cmd;
                                daySelected(daySelectedCmd.getWeek(), daySelectedCmd.getDay());
                        }
                        if (cmd.getLabel().equals(Commands.Updated)){
                                update();
                        }
                        if (cmd.getLabel().equals(Commands.DayChoice)){
                            Dispatcher.getInstance().handleCommand(cmd);
                        }
                        if (cmd.getLabel().equals(Commands.PairView)){
                        	TimeOfTheClasses time = new TimeOfTheClasses(Integer.parseInt(new Character(super.getString(super.getSelectedIndex()).charAt(0)).toString()));
                        	Dispatcher.getInstance().handleCommand(new PairSelectedCommand(time));
                            Dispatcher.getInstance().handleCommand(cmd);
                        }
                        if (cmd.getLabel().equals(Commands.Edit)){
                        	TimeOfTheClasses time = new TimeOfTheClasses(Integer.parseInt(new Character(super.getString(super.getSelectedIndex()).charAt(0)).toString()));
                        	Dispatcher.getInstance().handleCommand(new PairSelectedCommand(time));
                        	Dispatcher.getInstance().handleCommand(cmd);
                        }
                        if (cmd.getLabel().equals(Commands.Remove)){
                        	TimeOfTheClasses time = new TimeOfTheClasses(Integer.parseInt(new Character(super.getString(super.getSelectedIndex()).charAt(0)).toString()));
                        	ScheduleEntry entry = ScheduleController.getInstance().getSchedule().getEntry(m_day, m_week, time);
                        	Dispatcher.getInstance().handleCommand(new RemoveEntryCommand(entry));
                        }
                        if (cmd.getLabel().equals(Commands.About)){
                        	Dispatcher.getInstance().handleCommand(cmd);
                        }
                        break;
                case Command.BACK :
                        Dispatcher.getInstance().handleCommand(new Command(Commands.Exit, Command.ITEM, 1));
                        break;
                }
        }
        
        private void daySelected(WeekDefinition week, DayOfWeek day){
                m_week = week;
                m_day = day;
                update();
        }
        
        private void update(){
        		
        		super.setTitle(m_day.getDayName()+" "+m_week.getWeekName());
        		
        		for(int i = 0; i < super.size(); ++i){
                        super.delete(i);   
                }
        		//super.deleteAll();
        		
                ScheduleEntry entriesToView[] = ScheduleController.getInstance().getSchedule().getEntriesForDay(m_day, m_week);
               
                for(int i = 0; i < entriesToView.length; ++i){
                        super.append(new Integer(entriesToView[i].getTime().getTimeNumber()).toString()+". "+entriesToView[i].getSubject().getDiscipline().getName(), null);
                }   
                
                if (super.size()!=0) 
                    addCommand(ReviewCmd);
                if (super.size()!=0) 
                    addCommand(EditCmd);
                if (super.size()!=0) 
                    addCommand(DeleteCmd);
        }
        
      /*  public void delete(){
        	TimeOfTheClasses time = new TimeOfTheClasses(Integer.parseInt(new Character(super.getString(super.getSelectedIndex()).charAt(0)).toString()));
    		ScheduleController.getInstance().getSchedule().deleteEntry(m_day, m_week, time);
            update();
        }*/
}
